package com.longmao.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.longmao.eduservice.entity.EduChapter;
import com.longmao.eduservice.entity.EduVideo;
import com.longmao.eduservice.entity.vo.ChapterVo;
import com.longmao.eduservice.entity.vo.VideoVo;
import com.longmao.eduservice.mapper.EduChapterMapper;
import com.longmao.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longmao.eduservice.service.EduVideoService;
import com.longmao.servicebase.handler.LongMaoException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author longmao
 * @since 2022-01-27
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;

    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //根据课程id 查询出课程里面的所有章节
        QueryWrapper<EduChapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id", courseId);
        chapterQueryWrapper.orderByAsc("sort");
        List<EduChapter> chapterList = baseMapper.selectList(chapterQueryWrapper);

        //根据课程id查询课程里面的所有小节
        QueryWrapper<EduVideo> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id", courseId);
        videoQueryWrapper.orderByAsc("sort");
        List<EduVideo> videoList = videoService.list(videoQueryWrapper);

        //创建list集合用户封装最终的数据
        List<ChapterVo> finalList = new ArrayList<>();

        //遍历查询章节list集合进行封装
        for (int i = 0; i < chapterList.size(); i++) {
            EduChapter eduChapter = chapterList.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, chapterVo);
            finalList.add(chapterVo);

            //创建集合用于封装章节中的小节
            List<VideoVo> voList = new ArrayList<>();

            //遍历查询章节list集合进行封装
            for (int j = 0; j < videoList.size(); j++) {
                EduVideo eduVideo = videoList.get(j);
                //判断小节中的chapter_id是否和章节中的id一样
                if(eduVideo.getChapterId().equals(eduChapter.getId())) {
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo, videoVo);
                    //放入小节集合中
                    voList.add(videoVo);
                }
            }

            //把封装之后的小节list集合放入章节对象中
            chapterVo.setChildren(voList);
        }

        return finalList;
    }

    @Override
    public boolean deleteChapterById(String chapterId) {
        //根据id查询是否存在视频小节，如果有则提示用户先删除小节视频课程
        if(videoService.getVideoByChapterId(chapterId)) {
            throw new LongMaoException(20001, "该章节下存在视频课程，请先删除视频课程");
        }

        Integer result = baseMapper.deleteById(chapterId);
        return null != result  &&  result > 0;
    }

    @Override
    public boolean deleteVideoByCourseId(String courseId) {
        QueryWrapper<EduChapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id", courseId);
        Integer result = baseMapper.delete(chapterQueryWrapper);
        return null != result && result > 0;
    }
}
